import { PageEnum } from '@/enums/router'
// import { AppRouteRecordRaw } from '@/router/types'
// import { renderIcon } from '@/utils/index'

/**
 * 排除Router
 **/
export function filterRouter(routerMap: Array<any>) {
  return routerMap.filter((item) => {
    return (
      (item.meta?.hidden || false) != true &&
      !['/:path(.*)*', '/', PageEnum.REDIRECT, PageEnum.BASE_LOGIN].includes(item.path)
    )
  })
}

/**
 * 递归组装菜单格式
 **/
export function generatorMenu(routerMap: Array<any>, isInner = false) {
  return filterRouter(routerMap).map((item) => {
    const children = item.children ? filterRouter(item.children) : []
    const isRoot = isInner ? false : children.length === 1
    const info = isRoot ? children[0] : item
    const currentMenu = {
      ...info,
      ...info.meta,
      label: info.meta?.title,
      key: info.name,
      icon: isRoot ? item.meta?.icon : info.meta?.icon
    }
    // 是否有子菜单，并递归处理
    if (info.children) {
      // Recursion
      currentMenu.children = generatorMenu(info.children)
    }
    return currentMenu
  })
}

/**
 * 过滤不是菜单的路由
 */
function filterMenus(list) {
  return list.filter((item) => {
    if (item.childrenList && item.childrenList.length > 0) {
      item.childrenList = filterRouter(item.childrenList)
    }
    return item.fuceLevel === 1
  })
}

export function generatorDynamicMenu(functionList: Array<any>, apiRouter: Array<any>) {
  const menus = filterMenus(functionList)
  console.log('menus', menus, apiRouter)
}

/**
 * 
 * /

/**
 * 路由权限判断
 */
export function routerPermissions(router, sysPermissions: Array<any>) {
  if (router.meta && router.meta.permissions) {
    try {
      let isAvailable = false
      for (const permission of router.meta.permissions) {
        if (sysPermissions.includes(permission)) {
          isAvailable = true
          break
        }
      }
      return isAvailable
    } catch (e) {
      return true
    }
  }
  return true
}
